Components.utils.import("resource://mldonkeyff/MLDonkeyDBConfig.js");
Components.utils.import("resource://mldonkeyff/MLDonkeyUtils.js");

mldonkeyff.onFirefoxLoad = function(event) {
  document.getElementById("contentAreaContextMenu")
          .addEventListener("popupshowing", function (e){ mldonkeyff.showFirefoxContextMenu(e); }, false);
};

mldonkeyff.showFirefoxContextMenu = function(event) {
  // show or hide the menuitem based on what the context menu is on
  document.getElementById("context-mldonkeyff").hidden = gContextMenu.onImage;
};

mldonkeyff.perServerMenuCreator = function(id, client) {
	var menupopup = document.createElement("menupopup");
	menupopup.setAttribute("id", "server_menupopup_" + id);
	//
	if(client.isConnected()) {
		var menuItem = null;
		//
		menuItem = document.createElement("menuitem");
		menuItem.setAttribute("label", "Downloads...");
		//menuItem.addEventListener("click", client.connect, true);
		menupopup.appendChild(menuItem);
		//
		menuItem = document.createElement("menuitem");
		menuItem.setAttribute("label", "Servers...");
		//menuItem.addEventListener("click", client.connect, true);
		menupopup.appendChild(menuItem);
		//
		menuItem = document.createElement("menuitem");
		menuItem.setAttribute("label", "Disconnect");
		menuItem.addEventListener("click", function() { client.disconnect(); }, true);
		menupopup.appendChild(menuItem);
	} else {
		var menuItem = document.createElement("menuitem");
		menuItem.setAttribute("label", "Connect");
		menuItem.addEventListener("click", function() { client.connect(); }, true);
		//
		menupopup.appendChild(menuItem);
	}
	//
	var menu = document.createElement("menu");
	menu.setAttribute("label", client.getIdentifier());
	menu.appendChild(menupopup);
	//
	return menu;
};

mldonkeyff.onStatusBarMenuPopupShowing = function(event) {
	var mldonkeyffStatusbarContextMenu = document.getElementById("mldonkeyffStatusbarContextMenu");
	if(event.target.id == mldonkeyffStatusbarContextMenu.id) {
		var separatoreBeforePreferenceMenuItem = document.getElementById("separatoreBeforePreferenceMenuItem");
		var clients = MLDonkeyDBConfig.getConfiguredClients();
		// Clean the menu
		while(separatoreBeforePreferenceMenuItem.previousSibling != null) {
			mldonkeyffStatusbarContextMenu.removeChild(separatoreBeforePreferenceMenuItem.previousSibling);
		}
		//
		if(clients.length > 0) {
			for(clientId in clients) {
				var client = clients[clientId];
				//
				mldonkeyffStatusbarContextMenu.insertBefore(mldonkeyff.perServerMenuCreator(clientId, client), separatoreBeforePreferenceMenuItem);
			}
		} else {
			var menuItem = document.createElement("menuitem");
			menuItem.setAttribute("label", "Use 'Preferences...' to configure some server");
			menuItem.setAttribute("disabled", true);
			//
			mldonkeyffStatusbarContextMenu.insertBefore(menuItem, separatoreBeforePreferenceMenuItem);
		}
	}
};

window.addEventListener("load", function () { mldonkeyff.onFirefoxLoad(); }, false);
window.addEventListener("unload", function () { MLDonkeyDBConfig.destroy(); }, false);
